import util, pygame

class GameObject( pygame.sprite.Sprite ):
    """
    GameObject is an extension of the PyGame Sprite class with added functionality.
    """
    def __init__(self):
        """
        We want PyGame's Sprite class to integrate seamlessly.
        """
        pygame.sprite.Sprite.__init__(self)
    
    def detectCollision(self, groups):
        """
        Returns a list of sprites from groups that sprite is colliding with (excluding
        self).
        """
        collideList = []
        # Perform rect collision first.
        for group in groups:
            collideList.extend( pygame.sprite.spritecollide(self, group, False) )
        # Keep only the sprites that mask-collide and exclude self.
        return filter(lambda s: pygame.sprite.collide_mask(self, s) and self != s, collideList)
    
    def update(self):
        """
        Updates the state and graphics variables.
        """
        util.notDefined()

    def clear(self):
        """
        Clears the piece of background behind the current rect to its original state.
        """
        util.notDefined()

    def draw(self):
        """
        Draws the right images and rectangles. Usually at least self.image and 
        self.rect.
        """
        util.notDefined()

class ObjectGroup( pygame.sprite.Group ):
    """
    ObjectGroup is an extension of the PyGame Group class with slight changes.
    """
    def __init__(self):
        """
        We want PyGame's Group class to integrate seamlessly.
        """
        pygame.sprite.Group.__init__(self)

    def update(self, previousTime, currentTime):
        """
        Calling this method properly clears, updates and draws the GameObjects
        contained within.
        """
        # Order matters! Leave these loops as they are, otherwise there will be 
        # drawing residue all over.
        for o in self:
            o.clear()
            o.update(previousTime, currentTime)
        for o in self:
            o.draw()
